package com.example.demo.mapper.sysnotice;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.bean.PageRes;
import com.example.demo.bean.SortParam;
import com.example.demo.bean.sysnotice.SysNoticeDO;
import com.example.demo.mapper.BaseMapperX;
import com.example.demo.dto.sysnotice.SysNoticePageReqVO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface SysNoticeMapper extends BaseMapperX<SysNoticeDO> {

    default PageRes<SysNoticeDO> selectPage(SysNoticePageReqVO reqVO) {
        Page<SysNoticeDO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        QueryWrapper<SysNoticeDO> queryWrapper = new QueryWrapper<SysNoticeDO>()
                .like(ObjectUtil.isNotNull(reqVO.getNoticeTitle()), "notice_title", reqVO.getNoticeTitle())
                .eq(ObjectUtil.isNotNull(reqVO.getNoticeType()),"notice_type",reqVO.getNoticeType())
                .eq(ObjectUtil.isNotNull(reqVO.getStatus()),"status",reqVO.getStatus())
                .between(
                        ObjectUtil.isNotNull(reqVO.getTimeArr()),
                        "create_time",
                        ArrayUtil.get(reqVO.getTimeArr(), 0),
                        ArrayUtil.get(reqVO.getTimeArr(), 1)
                );
        // 添加排序条件
        List<SortParam> sortMap = reqVO.getSortMap();
        if (ObjectUtil.isNotNull(sortMap) && !sortMap.isEmpty()) {
            for (SortParam sortParam : sortMap) {
                String field = sortParam.getSortField();
                String type = sortParam.getSortType();
                if (ObjectUtil.isNotNull(field) && ObjectUtil.isNotNull(type)) {
                    if ("asc".equalsIgnoreCase(type)) {
                        queryWrapper.orderByAsc(StrUtil.toUnderlineCase(field));
                    } else if ("desc".equalsIgnoreCase(type)) {
                        queryWrapper.orderByDesc(StrUtil.toUnderlineCase(field));
                    }
                }
            }
        }
        return selectPageCommon(page, queryWrapper);
    }
}
